﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using WoSoft.PACS.Data.CheckData;
using WoSoft.PACS.Data.DepartmentDoctor;
using WoSoft.PACS.Data.TemplateData;
using WoSoft.PACS.Document;
using DevComponents.DotNetBar.SuperGrid;

namespace WoSoft.PACS.Win.Forms.SubForms
{
    /// <summary>
    /// 检查报告
    /// </summary>
    public partial class FrmReadReport : FrmBase
    {
        private WordDocument wordDoc;
        //病人检查申请ID
        private string _checkappID;

        string str_jcyx = string.Empty;//检查印象
        string str_zdjy = string.Empty;//诊断建议
        string str_jcbz = string.Empty;//诊断建议

        //检查访问对象
        private CheckApp _checkapp;
        private TemplateDataService _templateDataService = new TemplateDataService();

        private ElementModel elementModel = new ElementModel();

        public FrmReadReport(string checkappid)
        {
            InitializeComponent();
            _checkappID = checkappid;
            _checkapp = new CheckApp();
        }

        //加载信息
        private void FrmCheckReport_Load(object sender, EventArgs e)
        {
            //结束word进程
            KillWorkProc();

            //加载报告模板
            LoadReport();

            //加载病人信息到报告中
            //LoadPatientInfo();

            //更新诊断建议
            wordDoc.AddWordText(str_jcyx, str_zdjy, str_jcbz);

            //锁定内容，不允许编辑
            try
            {
                var activeDoc = axFramerControl1.ActiveDocument as Microsoft.Office.Interop.Word.Document;
                activeDoc.Protect(Microsoft.Office.Interop.Word.WdProtectionType.wdAllowOnlyReading, true);
            }
            catch
            { 
            }
        }

        //结束word进程
        private void KillWorkProc()
        {
            var procs = Process.GetProcessesByName("WINWORD");
            if (procs.Length > 0)
            {
                procs[0].Kill();
            }
        }

        //加载报告模板
        private void LoadReport()
        {
            this.WindowState = FormWindowState.Maximized;
            try
            {
                dynamic obj = _checkapp.GetCheckReport(_checkappID);
                if (obj != null)
                {
                    str_jcyx = obj.检查印象;
                    str_zdjy = obj.诊断建议;
                    str_jcbz = obj.检查备注;
                    //加载模板 
                    this.axFramerControl1.CreateNew("Word.Document");
                    var docObj = this.axFramerControl1.ActiveDocument as Microsoft.Office.Interop.Word.Document;
                    if (docObj != null)
                    {
                        docObj.Content.InsertXML(obj.报告内容);
                    }
                }
                else
                {
                    MessageBoxEx.Show("该申请报告还未生成，请稍后查看！", "报告未生成");
                    this.Close();
                    return;
                }
                wordDoc = new WordDocument(this.axFramerControl1);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //加载病人信息到报到模板中
        private void LoadPatientInfo()
        {
            var dataRow = _checkapp.GetCheckAppByID(this._checkappID);
            if (dataRow != null)
            {
                elementModel.Name = dataRow["姓名"].ToString();
                if (dataRow["性别"] != null)
                {
                    var val = Convert.ToInt32(dataRow["性别"]);
                    elementModel.Sex = val == 1 ? "男" : "女";
                }
                string nldw = string.Empty;
                switch (dataRow["年龄单位"].ToString())
                {
                    case "0":
                        nldw = "岁";
                        break;
                    case "1":
                        nldw = "月";
                        break;
                    case "2":
                        nldw = "天";
                        break;
                    case "3":
                        nldw = "小时";
                        break;
                    default:
                        nldw = "";
                        break;
                }
                elementModel.Age = dataRow["年龄"].ToString() + nldw;
                elementModel.AppDepartment = dataRow["申请科室"].ToString();
                elementModel.AppDoctor = dataRow["申请医生"].ToString();
                elementModel.AppOrderNum = dataRow["申请单号"].ToString();
                elementModel.BedNum = dataRow["床号"].ToString();
                elementModel.Diagnosis = dataRow["临床诊断"] != null ? dataRow["临床诊断"].ToString() : "";
                elementModel.CheckItem = dataRow["检查项目名称"] != null ? dataRow["检查项目名称"].ToString() : "";
                elementModel.CheckTime = dataRow["申请时间"] != null ? Convert.ToDateTime(dataRow["申请时间"]).ToString("yyyy-MM-dd") : string.Empty;
                var departid = dataRow["执行科室ID"] != null ? dataRow["执行科室ID"].ToString() : string.Empty;
                if (!string.IsNullOrEmpty(departid))
                {
                    DepartmentDoctorData department = new DepartmentDoctorData();
                    var departName = department.GetDepartNameById(departid);
                    elementModel.CheckDepartment = departName;
                }
                elementModel.CheckDoctor = dataRow["检查技师"] != null ? dataRow["检查技师"].ToString() : string.Empty;
                elementModel.CheckID = dataRow["检查号"].ToString();
                elementModel.AuditDoctor = dataRow["审核医师"] != null ? dataRow["审核医师"].ToString() : string.Empty;
                //传入检查报告中
                wordDoc.UpdatePatientInfos(elementModel,Program.yymc);
            }
        }

        //工具栏单击事件
        private void tlsReport_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            switch (e.ClickedItem.Name)
            {
                case "tlsPrint":
                    wordDoc.Print();
                    //更新打印状态
                    _checkapp.Update_SfPrint(_checkappID, 1);
                    break;
                case "tlsPrintView":
                    wordDoc.PrintView();
                    break;
                case "tlsSaveAs":
                    wordDoc.SaveAs();
                    break;
                case "tlsClose":
                    this.Close();
                    break;
                default:
                    break;
            }
        }
    }
}
